package com.ice.remoting.data;

import com.alibaba.fastjson.annotation.JSONField;

import java.util.concurrent.atomic.AtomicInteger;

public class RemotingCommand {

    private static final int RPC_TYPE = 0; // 0, REQUEST_COMMAND
    private static final int RPC_ONEWAY = 1; // 0, RPC

    public CommandCustomHeader commandCustomHeader;
    // 命令
    private byte command;

    private int flag = 0;
    // 原始body
    private transient byte[] body;
    // 事物ID标识
    private int opaque = requestId.getAndIncrement();

    private static AtomicInteger requestId = new AtomicInteger(0);

    public RemotingCommand() {
        this.commandCustomHeader = new CommandCustomHeader();
    }

    public byte getCommand() {
        return command;
    }

    public void setCommand(byte command) {
        this.command = command;
    }

    public byte[] getBody() {
        return body;
    }

    public void setBody(byte[] body) {
        this.body = body;
    }

    public void markOnewayRPC() {
        int bits = 1 << RPC_ONEWAY;
        this.flag |= bits;
    }

    @JSONField(serialize = false)
    public boolean isOnewayRPC() {
        int bits = 1 << RPC_ONEWAY;
        return (this.flag & bits) == bits;
    }

    public int getOpaque() {
        return opaque;
    }

    public void setOpaque(int opaque) {
        this.opaque = opaque;
    }

    public static int createNewRequestId() {
        return requestId.getAndIncrement();
    }

    public CommandCustomHeader getCommandCustomHeader() {
        return commandCustomHeader;
    }

    public void setCommandCustomHeader(CommandCustomHeader commandCustomHeader) {
        this.commandCustomHeader = commandCustomHeader;
    }
}
